import {queryAdminList, deleteAdmin, updateAdmin, saveAdmin, queryAdminMenu} from '../services/adminManageApi';

export default {
    namespace: 'adminManage',
    state: {
        loading: true,
        data: {
            list: [],
            pagination: {},
            adminMenu: []
        },
        adminMenuList: []
        // adminManageList: [],
    },
    effects: {
        * queryAdminMenu({payload, callback}, {call, put}) {
            const response = yield call(queryAdminMenu, payload);
            yield put({
                type: 'queryAdminMenuList',
                payload: response,
            });
            if (callback) callback(response);
        },
        * queryAdminList({payload}, {call, put}) {
            yield put({
                type: 'changeSubmitting',
                payload: true,
            });
            const response = yield call(queryAdminList, payload);
            yield put({
                type: 'queryAdminHandle',
                payload: response,
            });
            yield put({
                type: 'changeSubmitting',
                payload: false,
            })
        },
        * deleteAdmin({payload, callback}, {call, put}) {
            yield put({
                type: 'changeSubmitting',
                payload: true,
            });
            const response = yield call(deleteAdmin, payload);
            yield put({
                type: 'changeSubmitting',
                payload: false,
            });
            if (callback) callback(response);
        },
        * updateAdmin({payload, callback}, {call, put}) {
            yield put({
                type: 'changeSubmitting',
                payload: true,
            });
            const response = yield call(updateAdmin, payload);
            yield put({
                type: 'changeSubmitting',
                payload: false,
            });
            if (callback) callback(response);
        },
        * saveAdmin({payload, callback}, {call, put}) {
            yield put({
                type: 'changeSubmitting',
                payload: true,
            });
            const response = yield call(saveAdmin, payload);
            yield put({
                type: 'changeSubmitting',
                payload: false,
            });
            if (callback) callback(response);
        },
    },
    reducers: {
        changeSubmitting(state, {payload}) {
            return ({
                ...state,
                submitting: payload,
                loading: payload,
            })
        },
        queryAdminHandle(state, {payload}) {
            return ({
                ...state,
                data: payload.resultData.adminInfo,
                adminInfo: payload["resultData"]["adminInfo"]["list"],
                pagination: payload["resultData"]["adminInfo"]["pagination"],
            })
        },
        queryAdminMenuList(state, {payload}) {
            return ({
                ...state,
                adminMenuList: payload["resultData"]["adminMenu"],
            })
        }
    }
};